# encoding:utf-8
import random
import sys
from django.shortcuts import HttpResponse
import json
from IR.Util.Variables import Constant
from IR.Util.DB import DB
from  IR.moduels.snippet import Snippet
from  IR.moduels.Indexing import Search
import Util
reload(sys)
sys.setdefaultencoding('utf8')

class_mapping=dict()
class_mapping[u"国内"]="guonei"
class_mapping[u"国际"]="guoji"
class_mapping[u"社会"]="shehui"
class_mapping[u"体育"]="sports"
class_mapping[u"娱乐"]="ent"
class_mapping[u"军事"]="war"
class_mapping[u"科技"]="tech"
class_mapping[u"财经"]="money"


def getClass(request):
    if (request.method == "POST"):
        key_word = request.POST.get(Constant.KEYWORD, "")
        print (key_word)
        # key_word=key_word.decode("UTF-8")

        #pagenum从0开始
        page_num = int(request.POST.get(Constant.PAGE_NUM, 0))
        count = int(request.POST.get(Constant.COUNT, 20))
        sort_mode=int(request.POST.get(Constant.SORT_MODE, 0))
        result = dict()
        word_slot_dic = dict()
        if (key_word == "" or len(key_word) <= 0):
            result[Constant.ERROR_CODE] = 1
            result[Constant.ERROR_MESSAGE] = "key_word is null"
            result[Constant.STATUS] = Constant.FAIL
            return HttpResponse(json.dumps(result), content_type="application/json")
        res_list=None
        try:
            db=DB()
            if(class_mapping.has_key(key_word)):
                key_word=class_mapping[key_word]
            collection = db.connection('wangyi_news')
            news = collection.find({"news_first_category":key_word}).sort([("news_craw_time", -1)]).skip(page_num*count).limit(count)
            if (news != None):
                res_list = list(news)
            else:
                print ("返回为空")
        except Exception,e:
            print (e.message)
        if (res_list != None):
            news_list=list()
            for item in res_list:
                news=dict()
                news["relativity"] = ""  # 相关度
                time=str(item["news_pub_time"]).replace('/','-')
                time=time[6:10]+"-"+time[:5]+" "+time[10:]
                news["time"] = time

                news["content"] = item["news_content"]  # 内容
                news["title"] = item["news_title"]  # 标题
                news["keywords"] = item["news_keyword"]  # 关键词
                news["comment_num"] = item["news_comment_num"]  # 评论数
                news["url"] = item["news_url"]  # 网页链接
                news["source"] = item["news_source_website"]
                snippet = Snippet()
                 # 生成预览
                IDs = list()
                IDs.append(str(item["news_id"]))

                preview = snippet.create_preview(IDs)
                news["preview"] = preview  # 预览
                news["commendatory_num"] = "暂无"  # 褒义评论数
                news["snippet"] = ""
                news["relativity_news"] = "暂无"  # 相关新
                news_list.append(news)
            news_list=Util.sort_news(news_list,sort_mode)
            result[Constant.STATUS] = Constant.SUCCESS
            result[Constant.NWES_LIST] =news_list
            return HttpResponse(json.dumps(result), content_type="application/json")
        else:
            result[Constant.STATUS] = Constant.FAIL
            result[Constant.ERROR_CODE] = 2
            result[Constant.ERROR_MESSAGE] = "fail to get data from mongodb"
            return HttpResponse(json.dumps(result), content_type="application/json")